/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package devbureau.servlet;

import devbureau.fstore.common.StoreFactory;
import devbureau.fstore.common.report.Bill;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import static devbureau.fstore.util.Utilities.DateTime.getStartDate;
import static devbureau.fstore.util.Utilities.DateTime.parseShort;
import static devbureau.fstore.util.Utilities.DateTime.formatLong;

/**
 *
 * @author admin
 */
public class BillReportServlet extends HttpServlet {
    private static final Logger log = Logger.getLogger(BillReportServlet.class);
    private static final SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Date from = parseShort(request.getParameter(Constants.FROM));
        Date to = parseShort(request.getParameter(Constants.TO));
        
        try{
            from = getStartDate(from);
            to = getStartDate(to);
            List<Bill> bills = new ArrayList<Bill>();
            log.debug("getBillReport from["+formatLong(from)+"] to["+formatLong(to)+"]");
            bills = StoreFactory.getStore().getBillReport(from, to);
            log.debug("bills report size: " + bills.size());
            request.setAttribute(Constants.BILL_REPORTS, bills);
            request.setAttribute(Constants.FROM, from);
            request.setAttribute(Constants.TO, to);
        }
        catch(Exception e){
            log.error("Error while getting bill report");
            request.setAttribute(Constants.MESSAGE_ERROR, e.getMessage());
        }
        request.getRequestDispatcher("reports.jsp").forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>


}
